﻿Imports System.Drawing
Imports System.IO
Imports System.Net
Imports System.Text
Imports Newtonsoft.Json

Module TenApi
    Public ReadOnly Lazyclient As New Lazy(Of WebClient)

    Function 获取IP地址() As Dictionary(Of String, Object)
        Dim str = Lazyclient.Value.DownloadString("https://tenapi.cn/v2/getip")
        Return JsonConvert.DeserializeObject(Of Dictionary(Of String, Object))(str)
    End Function

    ''' <summary>
    ''' 返回字典结构如下
    '''"code": 200,
    '''"tel" "18888888888",
    '''"local": "归属地：北京市",
    '''"duan": "号码段：1888888",
    '''"type": "卡类型：北京移动TD-SCDMA卡 (3G)",
    '''"yys": "运营商：中国移动",
    '''"bz": "通信标准：TD-SCDMA (时分同步码分多址)"
    ''' </summary>
    ''' <param name="手机号">要查询的手机号码</param>
    ''' <returns></returns>
    Function 手机号归属地查询(手机号 As String) As Dictionary(Of String, Object)
        Dim uri As New Uri($"https://tenapi.cn/tel/?tel={手机号}")
        Dim request As HttpWebRequest = WebRequest.Create(uri)
        Dim response As HttpWebResponse = request.GetResponse()
        Using reader As New StreamReader(response.GetResponseStream(), Encoding.UTF8)
            Return JsonConvert.DeserializeObject(Of Dictionary(Of String, Object))(reader.ReadToEnd)
        End Using
    End Function

    Function QQ头像获取(QQ号 As String) As Bitmap
        'Dim uri As New Uri($"https://tenapi.cn/qqimg/?qq={QQ号}")
        'Dim request As HttpWebRequest = WebRequest.Create(uri)
        'Dim response As HttpWebResponse = request.GetResponse()
        'Dim stream = response.GetResponseStream()
        'Dim bm As New Bitmap(stream)
        Dim data = Lazyclient.Value.DownloadData($"https://tenapi.cn/qqimg/?qq={QQ号}")
        Return New Bitmap(New MemoryStream(data))
    End Function

    Function QQ群头像获取(群号 As String) As Bitmap
        Dim uri As New Uri($"https://tenapi.cn/qunimg/?qun={群号}")
        Dim request As HttpWebRequest = WebRequest.Create(uri)
        Dim response As HttpWebResponse = request.GetResponse()
        Dim stream = response.GetResponseStream()
        Dim bm As New Bitmap(stream)
        Return bm
    End Function

    Function 随机二次元动漫图片() As Bitmap
        Dim uri As New Uri($"https://tenapi.cn/acg")
        Dim request As HttpWebRequest = WebRequest.Create(uri)
        Dim response As HttpWebResponse = request.GetResponse()
        Dim stream = response.GetResponseStream()
        Dim bm As New Bitmap(stream)
        Return bm
    End Function

    Function 一言随机输出一句话() As String
        Dim uri As New Uri($"https://tenapi.cn/yiyan/")
        Dim request As HttpWebRequest = WebRequest.Create(uri)
        Dim response As HttpWebResponse = request.GetResponse()
        Dim stream = response.GetResponseStream()
        Using reader As New StreamReader(stream)
            Return reader.ReadToEnd
        End Using
    End Function

End Module